﻿using Mt.Site.Core.Organizations.Posts;
using Mt.Site.Core.Organizations.UserCache.Models;

namespace Mt.Site.Core.Organizations.UserCache
{
    public static class UserAndUnitFinderHelper
    {
        #region 人员等级判别

        /// <summary>
        /// 普通职员
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <returns></returns>
        public static bool IsMember(this UserInfoDto userInfoDto)
        {
            return userInfoDto.NewPostLevel == NewPostLevel.Member;
        }

        /// <summary>
        /// 分管领导
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <returns></returns>
        public static bool IsResponsibleLeader(this UserInfoDto userInfoDto)
        {
            return userInfoDto.NewPostLevel == NewPostLevel.ResponsibleLeader;
        }


        /// <summary>
        /// 总经理
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <returns></returns>
        public static bool IsGeneralManager(this UserInfoDto userInfoDto)
        {
            return userInfoDto.NewPostLevel == NewPostLevel.GeneralManager;
        }
        
        /// <summary>
        /// 董事长
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <returns></returns>
        public static bool IsChairman(this UserInfoDto userInfoDto)
        {
            return userInfoDto.NewPostLevel == NewPostLevel.Chairman;
        }

        /// <summary>
        /// 总经理 或 董事长
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <returns></returns>
        public static bool IsMainLeaders(this UserInfoDto userInfoDto)
        {
            return userInfoDto.IsGeneralManager() || userInfoDto.IsChairman();
        }


        /// <summary>
        /// 主部门中 担任 办公室主任，部门主管，部门副主管，子公司正职，子公司副职
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <returns></returns>
        public static bool IsMidManager(this UserInfoDto userInfoDto)
        {
            return (userInfoDto.NewPostLevel == NewPostLevel.MidManager || userInfoDto.NewPostLevel == NewPostLevel.MidDeputyManager || userInfoDto.NewPostLevel == NewPostLevel.MidDeputyManagerPresiding);
        }

        /// <summary>
        /// 主部门中 担任 部门主管(或分公司正职)
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <returns></returns>
        public static bool IsMidLeader(this UserInfoDto userInfoDto)
        {
            return userInfoDto.NewPostLevel == NewPostLevel.MidManager;
        }

        /// <summary>
        /// 主部门中 担任 部门副主管主持工作(或分公司副职主持工作)
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <returns></returns>
        public static bool IsMidDeputyPresiding(this UserInfoDto userInfoDto)
        {
            return userInfoDto.NewPostLevel == NewPostLevel.MidDeputyManagerPresiding;
        }

        /// <summary>
        /// 主部门中 担任 部门副主管非主持工作(或分公司副职非主持工作)
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <returns></returns>
        public static bool IsMidDeputyNotPresiding(this UserInfoDto userInfoDto)
        {
            return userInfoDto.NewPostLevel == NewPostLevel.MidDeputyManager;
        }

        /// <summary>
        /// 主部门中 担任 办公室主任
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <returns></returns>
        public static bool IsOfficeDirector(this UserInfoDto userInfoDto)
        {
            if (userInfoDto.IsMidLeader())
            {
                foreach (var item in userInfoDto.Posts)
                {
                    if (item.OrganizationId == userInfoDto.MainOrgnzation?.Id
                        && item.PostLevel == PostGroup.OfficeDirector)
                    {
                        return true;
                    }
                }

            }

            return false;
        }

        public static bool IsOfficeDirectorOfSubsidiary(this UserInfoDto userInfoDto)
        {
            foreach (var item in userInfoDto.Posts)
            {
                if (item.OrganizationId == userInfoDto.MainOrgnzation?.Id
                    && item.PostLevel == PostGroup.OfficeDirectorOfSubsidiary)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 主部门中 担任 子公司正职领导
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <returns></returns>
        public static bool IsLeaderOfSubsidiary(this UserInfoDto userInfoDto)
        {
            if (userInfoDto.IsMidLeader())
            {
                foreach (var item in userInfoDto.Posts)
                {
                    if(item.OrganizationId == userInfoDto.MainOrgnzation?.Id 
                        && item.PostLevel == PostGroup.LeaderOfSubsidiary)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// 主部门中 担任 副职领导主持工作
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <returns></returns>
        public static bool IsDeputyLeaderOfSubsidiaryPresiding(this UserInfoDto userInfoDto)
        {
            if (userInfoDto.IsMidDeputyPresiding())
            {
                foreach (var item in userInfoDto.Posts)
                {
                    if (item.OrganizationId == userInfoDto.MainOrgnzation?.Id
                        && item.PostLevel == PostGroup.DeputyLeaderOfSubsidiary
                        && item.DeputyPresiding)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// 主部门中 担任 副职领导非主持工作
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <returns></returns>
        public static bool IsDeputyLeaderOfSubsidiaryNotPresiding(this UserInfoDto userInfoDto)
        {
            if (userInfoDto.IsMidDeputyNotPresiding())
            {
                foreach (var item in userInfoDto.Posts)
                {
                    if (item.OrganizationId == userInfoDto.MainOrgnzation?.Id
                        && item.PostLevel == PostGroup.DeputyLeaderOfSubsidiary
                        && !item.DeputyPresiding)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// 主部门在分公司 中层领导 正职或副职 
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public static bool IsOnlyMidLeaderOfSubsidiary(this UserInfoDto userInfoDto)
        {
            var mainUnitId = userInfoDto.MainOrgnzation?.Id;
            if (mainUnitId == null) return false;
            if (userInfoDto.IsMidManager())
            {
                return IsLeaderManagerOfSubsidiaryByMainUnit(userInfoDto, mainUnitId.Value);
            }
            return false;
        }
        #endregion


        /// <summary>
        /// 确认是否是某部门的主管  或 正职
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <param name="unitId"></param>
        /// <returns></returns>
        public static bool IsDepartmentHead(this UserInfoDto userInfoDto, long unitId)
        {
            foreach (var item in userInfoDto.Posts)
            {
                if (item.OrganizationId == unitId)
                {
                    if (item.PostLevel == PostGroup.DepartmentHead || item.PostLevel == PostGroup.LeaderOfSubsidiary || item.PostLevel == PostGroup.OfficeDirector)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// 确认是否是某部门(或分公司)的主管或主持工作的副主管
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <param name="unitId"></param>
        /// <returns></returns>
        public static bool IsDepartmentHeadOrDeputyHeadPresiding(this UserInfoDto userInfoDto, long unitId)
        {
            foreach (var item in userInfoDto.Posts)
            {
                if (item.OrganizationId == unitId)
                {
                    if (item.PostLevel == PostGroup.DepartmentDeputyManager && item.DeputyPresiding)
                    {
                        return true;
                    }
                    else if (item.PostLevel == PostGroup.DepartmentHead)
                    {
                        return true;
                    }
                    else if (item.PostLevel == PostGroup.OfficeDirector)
                    {
                        return true;
                    }
                    else if (item.PostLevel == PostGroup.DeputyLeaderOfSubsidiary && item.DeputyPresiding)
                    {
                        return true;
                    }
                    else if (item.PostLevel == PostGroup.LeaderOfSubsidiary)
                    {
                        return true;
                    }
                }
            }

            return false;
        }


        public static bool IsDepartmentHeadOrDeputyHead(this UserInfoDto userInfoDto, long unitId)
        {
            foreach (var item in userInfoDto.Posts)
            {
                if (item.OrganizationId == unitId)
                {
                    if (item.PostLevel == PostGroup.DepartmentDeputyManager)
                    {
                        return true;
                    }
                    else if (item.PostLevel == PostGroup.DepartmentHead)
                    {
                        return true;
                    }
                    else if (item.PostLevel == PostGroup.OfficeDirector)
                    {
                        return true;
                    }
                    else if (item.PostLevel == PostGroup.DeputyLeaderOfSubsidiary)
                    {
                        return true;
                    }
                    else if (item.PostLevel == PostGroup.LeaderOfSubsidiary)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// 主部门是否在分公司的正职 或副职
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <param name="unitId"></param>
        /// <returns></returns>
        public static bool IsLeaderManagerOfSubsidiaryByMainUnit(this UserInfoDto userInfoDto,long unitId)
        {
            foreach (var item in userInfoDto.Posts)
            {
                if (item.OrganizationId == unitId)
                {
                    if (item.PostLevel == PostGroup.LeaderOfSubsidiary 
                        || item.PostLevel == PostGroup.DeputyLeaderOfSubsidiary)
                    {
                        return true;
                    }
                }
            }

            return false;
        }
    }
}
